/**
 * 医院建筑数据操作API
 * 基于IndexedDB的医院建筑管理功能
 */

import { hospitalDB, HospitalBuildingData } from '../indexedDB'

export interface CreateHospitalBuildingRequest {
  name: string
  description: string
  hospitalId: string
  center: [number, number]
  bounds: {
    north: number
    south: number
    east: number
    west: number
  }
  modelPath: string
  floors?: string[]
  metadata?: Record<string, any>
}

export interface UpdateHospitalBuildingRequest extends Partial<CreateHospitalBuildingRequest> {
  id: string
}

export interface HospitalBuildingResponse {
  success: boolean
  msg: string
  data?: HospitalBuildingData
}

export interface HospitalBuildingsListResponse {
  success: boolean
  msg: string
  data?: HospitalBuildingData[]
}

/**
 * 创建医院建筑
 */
export const createHospitalBuilding = async (
  building: CreateHospitalBuildingRequest
): Promise<HospitalBuildingResponse> => {
  try {
    const data = await hospitalDB.addHospitalBuilding({
      ...building,
      floors: building.floors || [],
      metadata: building.metadata || {}
    })
    
    return {
      success: true,
      msg: '医院建筑创建成功',
      data
    }
  } catch (error) {
    console.error('创建医院建筑失败:', error)
    return {
      success: false,
      msg: '创建医院建筑失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 获取所有医院建筑列表
 */
export const getHospitalBuildingsList = async (): Promise<HospitalBuildingsListResponse> => {
  try {
    const data = await hospitalDB.getHospitalBuildings()
    return {
      success: true,
      msg: '获取医院建筑列表成功',
      data
    }
  } catch (error) {
    console.error('获取医院建筑列表失败:', error)
    return {
      success: false,
      msg: '获取医院建筑列表失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 根据医院ID获取建筑列表
 */
export const getHospitalBuildingsByHospital = async (
  hospitalId: string
): Promise<HospitalBuildingsListResponse> => {
  try {
    const data = await hospitalDB.getHospitalBuildingsByHospital(hospitalId)
    return {
      success: true,
      msg: '获取医院建筑列表成功',
      data
    }
  } catch (error) {
    console.error('获取医院建筑列表失败:', error)
    return {
      success: false,
      msg: '获取医院建筑列表失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 根据ID获取医院建筑详情
 */
export const getHospitalBuildingById = async (id: string): Promise<HospitalBuildingResponse> => {
  try {
    const data = await hospitalDB.getHospitalBuilding(id)
    if (!data) {
      return {
        success: false,
        msg: '医院建筑不存在'
      }
    }
    
    return {
      success: true,
      msg: '获取医院建筑详情成功',
      data
    }
  } catch (error) {
    console.error('获取医院建筑详情失败:', error)
    return {
      success: false,
      msg: '获取医院建筑详情失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 更新医院建筑信息
 */
export const updateHospitalBuilding = async (
  building: UpdateHospitalBuildingRequest
): Promise<HospitalBuildingResponse> => {
  try {
    // 先获取现有数据
    const existing = await hospitalDB.getHospitalBuilding(building.id)
    if (!existing) {
      return {
        success: false,
        msg: '医院建筑不存在'
      }
    }
    
    // 合并更新数据
    const updatedData: HospitalBuildingData = {
      ...existing,
      ...building,
      updatedAt: new Date()
    }
    
    await hospitalDB.updateHospitalBuilding(updatedData)
    
    return {
      success: true,
      msg: '医院建筑更新成功',
      data: updatedData
    }
  } catch (error) {
    console.error('更新医院建筑失败:', error)
    return {
      success: false,
      msg: '更新医院建筑失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 删除医院建筑
 */
export const deleteHospitalBuilding = async (id: string): Promise<HospitalBuildingResponse> => {
  try {
    // 检查医院建筑是否存在
    const existing = await hospitalDB.getHospitalBuilding(id)
    if (!existing) {
      return {
        success: false,
        msg: '医院建筑不存在'
      }
    }
    
    await hospitalDB.deleteHospitalBuilding(id)
    
    return {
      success: true,
      msg: '医院建筑删除成功'
    }
  } catch (error) {
    console.error('删除医院建筑失败:', error)
    return {
      success: false,
      msg: '删除医院建筑失败: ' + (error instanceof Error ? error.message : '未知错误')
    }
  }
}

/**
 * 预设医院建筑模型选项
 */
export const getPresetHospitalModels = () => {
  return [
    {
      label: '综合医院大楼',
      value: '/models/hospital_main.glb',
      description: '多层综合医院主楼建筑'
    },
    {
      label: '门诊楼',
      value: '/models/hospital_outpatient.glb',
      description: '门诊专用楼建筑'
    },
    {
      label: '急诊楼',
      value: '/models/hospital_emergency.glb',
      description: '急诊科专用楼'
    },
    {
      label: '住院楼',
      value: '/models/hospital_inpatient.glb',
      description: '住院部楼建筑'
    },
    {
      label: '医技楼',
      value: '/models/hospital_medical_tech.glb',
      description: '医疗技术科室楼'
    }
  ]
}

/**
 * 预设楼层选项
 */
export const getPresetFloors = () => {
  return [
    'B2', 'B1', 'L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10'
  ]
}